package ged.extension.point.definition.handlers;

import ged.extension.point.definition.IImageFigureFactory;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.gmf.runtime.draw2d.ui.render.figures.ScalableImageFigure;
import org.osgi.framework.Bundle;

public class EvaluateCintributionsForImages {

	//Access any extensions that have been created for this extension point
	private static final String ICUSTOM_IMAGE_ID = 
				"ged.extension.point.definition.images";
	private static EvaluateCintributionsForImages instance = null;
	static Map<String, ScalableImageFigure> map=new HashMap<String, ScalableImageFigure>();
	ScalableImageFigure newImage = null;
	
	protected EvaluateCintributionsForImages()
	{
		
	}
	
	public static EvaluateCintributionsForImages getInstance()
	{
		if(instance == null)
		{
			instance = new EvaluateCintributionsForImages();
		}
		return instance;
	}
	
	public Map<String, ScalableImageFigure> evaluateContibutedImages() {
		//Extension Registry - API which returns the extension points - Get Extension Registry
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		//Read all the extension
		/**
		 * Configuration elements are object representations of the XML elements under the <extension> tag in the plugin.xml files
		 */
		IConfigurationElement[] config = registry
				.getConfigurationElementsFor(ICUSTOM_IMAGE_ID);
		Bundle returnedBundle = null;
		Path returnedPath = null;
		for (int i = 0; i < config.length; i++) 
		{
			IConfigurationElement element = config[i];
			String name = element.getAttribute("name");
			String uniqueID = element.getAttribute("id");
			try {
				final Object factory = element.createExecutableExtension("class");
				if(((IImageFigureFactory) factory).canCreate(name))
				{
					returnedBundle = ((IImageFigureFactory) factory).createBundle();
					returnedPath = ((IImageFigureFactory) factory).createPath();
					if(returnedBundle != null || returnedPath != null)
					{
						URL Image_url = FileLocator.find(returnedBundle, returnedPath, null);
						newImage = new ScalableImageFigure(org.eclipse.gmf.runtime.draw2d.ui.render.factory.RenderedImageFactory
								.getInstance(Image_url), true, true, true);
					}
					else 
						newImage = null;
				}
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			map.put(name, newImage);	
		}
		return map;		
	}
	
}

/*String icon = element.getAttribute("icon");
producedimagePath = new Path(icon);
URL Image_url = FileLocator.find(bundle, producedimagePath, null);
ScalableImageFigure newImage = new ScalableImageFigure(org.eclipse.gmf.runtime.draw2d.ui.render.factory.RenderedImageFactory
				.getInstance(Image_url), true, true, true);*/
